Skip to content

[cookbook] fixed file uploads with Doctrine samples to upload the file wh #564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 30, 2011

Conversation

hhamon
Copy link
Contributor

@hhamon hhamon commented Jul 20, 2011

[cookbook] fixed file uploads with Doctrine samples to upload the file when the object is both persisted and updated. Added a note about PrePersist, PreUpdate, PostPersist and PostUpdate lifecycle callback events.

The ``@ORM\PrePersist()`` and ``@ORM\PostPersist()`` event callbacks are
triggered before and after the entity is persisted to the database. On the
other hand, the ``@ORM\PreUpdate()`` and ``@ORM\PostUpdate()`` event
callbacks are called when the entity is udpated.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably meant "updated" :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks fixed.

@weaverryan
Copy link
Member

Hey Hugo!

Ok, so you're definitely correct with this PR, but to be a truly functional system, should we also include something that would handle unlinking the old file when a new file is updated? I believe with this, each time you "update" the record by uploading a new file, the old file will still remain on the filesystem.

Thoughts?

Hugo Hamon added 2 commits July 27, 2011 08:42
…e when the object is both persisted and updated. Added a note about PrePersist, PreUpdate, PostPersist and PostUpdate lifecycle callback events.
@hhamon
Copy link
Contributor Author

hhamon commented Jul 27, 2011

Hi Ryan,

I'm ready with this PR. I have fixed it and tested it while I was giving a training. We were trying the file uploads with Doctrine and we noticed the update was not proceeded because of the missing @PreUpdate and @postupdate lifecycle callbacks.

You can merge the PR and then I will open a new PR to remove the old file on the filesystem in case of the entity is updated with a new file upload with a different name.

Cheers.

@hhamon
Copy link
Contributor Author

hhamon commented Jul 28, 2011

In fact, don't merge it now... I need to recheck the code.

@hhamon
Copy link
Contributor Author

hhamon commented Jul 28, 2011

So I rechecked and the code is good but there is a "bug" in Doctrine2. The pre and post update callbacks are only called if at least one property of the object has changed. If we try to only change the uploaded file without changing the other properties, the callbacks are not called... and that sucks...

I will add a note in the PR.

@stof
Copy link
Member

stof commented Jul 28, 2011

it is not a bug but a feature. The update event is dispatched only if the entity is updated, not for all entities that were queried during the request.

@hhamon
Copy link
Contributor Author

hhamon commented Jul 28, 2011

Yes it's a feature and a quite logic behavior. Nevertheless, if we want to trigger the callbacks we have to use a hack. For example, we can rely on an updatedAt field in the entity that is changed everytime the file is uploaded.

public function setFile(UploadedFile $file)
{
    $this->file = $file;
    $this->updatedAt = new \DateTime();
}

But it's still a hack and it's probably not the best solution to consider the lifecycle callbacks when dealing with file uploads and doctrine entities...

weaverryan added a commit that referenced this pull request Jul 30, 2011
[cookbook] fixed file uploads with Doctrine samples to upload the file wh
@weaverryan weaverryan merged commit 4f89114 into symfony:master Jul 30, 2011
@weaverryan
Copy link
Member

Hi Hugo!

Thanks for the good research, I understand the problem. I've merged in this PR - the entry is obviously not perfect yet, but it's better. I've added a caution message temporarily: 808da21

Perhaps this is a conversation for the dev-board. What we need is an agreed-upon "best practice" for this situation. So far, the example in this entry really doesn't fully solve the problem.

Thanks!

@weaverryan
Copy link
Member

Also see #600, so we can keep this on our radar.

@hhamon
Copy link
Contributor Author

hhamon commented Jul 31, 2011

Great! Thanks Ryan ;)

@lancergr
Copy link

I think that another solution that may be included in the documentation is to update the already existing path field instead of using "updatedAt":

public function setFile(UploadedFile $file) {
    if ($oldFile = $this->getAbsolutePath()) {
        unlink($oldFile);
    }

    $this->file = $file;
    $this->setPath("");
}

@murilolobato
Copy link
Contributor

I'm having a problem with the following situation:

On the edit screen of a entity that has OneToOne association to documents, if I click save without adding an image a register is created at the database on document, but path stays null, because i did not selected the image. What I want is dont create any entry on the 'document' table. Is there a way to do this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants